package com.itcast.springboot.service.impl;

import com.alibaba.fastjson.JSON;
import com.itcast.springboot.bean.Message;
import com.itcast.springboot.mapper.MessageMapper;
import com.itcast.springboot.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional
public class MessageServiceImpl implements MessageService {
    @Autowired
    MessageMapper messageMapper;
    //webSocketTemplate
    @Autowired
    private SimpMessagingTemplate template;

    @Override
    public String insertMessage(Message message) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  //设置日期格式
        try {
            message.setMdate(df.parse(df.format(new Date())));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (messageMapper.insertMessage(message) == 0) {
            return null;
        } else {
            Map<String, String> map = new HashMap<>();
            map.put("sendUserId", message.getSendUserId());
            map.put("receUserId", message.getReceUserId());
            map.put("msg", message.getMConn());
            //将map封装成JSON字符串
            String jsonMap = JSON.toJSONString(map);
            return jsonMap;
        }
    }

    @Override
    public Integer deleteMessage(Integer mid) {
        return messageMapper.deleteMessage(mid);
    }

    @Override
    public Message getMessageById(Integer mid) {
        return messageMapper.getMessageById(mid);
    }

    @Override
    public List<Message> getMessages(String uid, String fid) {
        return messageMapper.getMessages(uid, fid);
    }

    @Override
    public List<Message> getNewestMessages(String userId) {

        List<Message> messages = messageMapper.getNewestMessages(userId);
        if (messages.size() == 1 || messages.size() == 0){
            return messages;
        }
        //存储去掉同一个日期会出现多次rece_user_id和send_user_id多次的情况
        List<Message> messagesPlus = new ArrayList<>();
        //去掉同一个日期会出现多次rece_user_id和send_user_id多次的情况
        for (int i = 0;  i < messages.size() - 1; i++){
            String sid = messages.get(i).getSendUserId();
            String rid = messages.get(i).getReceUserId();
            String sidPlus = messages.get(i + 1).getSendUserId();
            String ridPlus = messages.get(i + 1).getReceUserId();
            if (sid.equals(sidPlus) && rid.equals(ridPlus) || sid.equals(ridPlus) && rid.equals(sidPlus)){
                continue;
            }
            messagesPlus.add(messages.get(i));
            if (i == messages.size() - 2){
                messagesPlus.add(messages.get(i + 1));
            }
        }
        int i = messages.size() - 2;
        String sid = messages.get(i).getSendUserId();
        String rid = messages.get(i).getReceUserId();
        String sidPlus = messages.get(i + 1).getSendUserId();
        String ridPlus = messages.get(i + 1).getReceUserId();
        if (sid.equals(sidPlus) && rid.equals(ridPlus)
                || sid.equals(ridPlus) && rid.equals(sidPlus)){
            messagesPlus.add(messages.get(i + 1));
        }
        return messagesPlus;
    }

    @Override
    public Integer updateMessage(Message message) {
        return messageMapper.updateMessage(message);
    }
}
